package com.ruoyi.store.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.store.domain.Payslip;
import com.ruoyi.store.service.IPayslipService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 工资条Controller
 *
 * @author ruoyi
 * @date 2023-05-02
 */
@Controller
@RequestMapping("/store/payslip")
public class PayslipController extends BaseController {
    private String prefix = "store/payslip";

    @Autowired
    private IPayslipService payslipService;

    @RequiresPermissions("store:payslip:view")
    @GetMapping()
    public String payslip() {
        return prefix + "/payslip";
    }

    /**
     * 查询工资条列表
     */
    @RequiresPermissions("store:payslip:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Payslip payslip) {
        startPage();
        List<Payslip> list = payslipService.selectPayslipList(payslip);
        return getDataTable(list);
    }

    /**
     * 导出工资条列表
     */
    @RequiresPermissions("store:payslip:export")
    @Log(title = "工资条", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Payslip payslip) {
        List<Payslip> list = payslipService.selectPayslipList(payslip);
        ExcelUtil<Payslip> util = new ExcelUtil<Payslip>(Payslip.class);
        return util.exportExcel(list, "工资条数据");
    }

    /**
     * 新增工资条
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 导入工资条
     *
     * @return
     */
    @GetMapping("/import")
    public String importExcel() {
        return prefix + "/import";
    }

    /**
     * 新增保存工资条
     */
    @Log(title = "工资条", businessType = BusinessType.INSERT)
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importExcel(Payslip payslip,@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        try {
            ExcelUtil excelUtil = new ExcelUtil(Payslip.class);
            List<Payslip> list = excelUtil.importExcel(file.getInputStream(), 0);
            payslip.setCash(list.stream().map(e-> Objects.isNull(e.getCash())?BigDecimal.ZERO:e.getCash()).reduce(BigDecimal.ZERO,BigDecimal::add));
            payslip.setRecharge(list.stream().map(e-> Objects.isNull(e.getRecharge())?BigDecimal.ZERO:e.getRecharge()).reduce(BigDecimal.ZERO,BigDecimal::add));
            payslip.setWorkMore(list.stream().map(e-> Objects.isNull(e.getWorkMore())?BigDecimal.ZERO:e.getWorkMore()).reduce(BigDecimal.ZERO,BigDecimal::add));
            payslip.setMember(list.stream().map(e-> Objects.isNull(e.getMember())?BigDecimal.ZERO:e.getMember()).reduce(BigDecimal.ZERO,BigDecimal::add));
            payslip.setMemberMore(list.stream().map(e-> Objects.isNull(e.getMemberMore())?BigDecimal.ZERO:e.getMemberMore()).reduce(BigDecimal.ZERO,BigDecimal::add));
            payslip.setTotalMoney(payslip.getCash().add(payslip.getMember()).add(payslip.getMemberMore()).add(payslip.getWorkMore()));
            payslip.setTotalGet(payslip.getCash().add(payslip.getRecharge()).add(payslip.getWorkMore()));
            payslipService.importExcel(payslip);
            JSONObject.toJSONString(payslip);
        } catch (Exception e) {
            return error(e.getMessage());
        }

        return success();
    }

    /**
     * 新增保存工资条
     */
    @RequiresPermissions("store:payslip:add")
    @Log(title = "工资条", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Payslip payslip) {
        return toAjax(payslipService.insertPayslip(payslip));
    }

    /**
     * 修改工资条
     */
    @RequiresPermissions("store:payslip:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Payslip payslip = payslipService.selectPayslipById(id);
        mmap.put("payslip", payslip);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资条
     */
    @RequiresPermissions("store:payslip:edit")
    @Log(title = "工资条", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Payslip payslip) {
        return toAjax(payslipService.updatePayslip(payslip));
    }

    /**
     * 删除工资条
     */
    @RequiresPermissions("store:payslip:remove")
    @Log(title = "工资条", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(payslipService.deletePayslipByIds(ids));
    }
}
